import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@Component
@WebFilter
public class JwtTokenFilter implements Filter {

    @Value("${jwt.secret}")
    private String secret;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // 从请求头中获取Token
        String token = // 从请求头中获取Token的代码，根据你的实际情况进行获取

        try {
            // 验证Token的合法性
            Claims claims = Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();

            // 根据需要进行其他验证逻辑

            // 将Token中的信息放入请求中
            // request.setAttribute("claims", claims);

            chain.doFilter(request, response);
        } catch (ExpiredJwtException e) {
            // Token过期
            // 处理过期的逻辑
        } catch (Exception e) {
            // Token验证失败
            // 处理验证失败的逻辑
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}
